package com.example.linuxchat.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.linuxchat.ChatActivity;
import com.example.linuxchat.R;
import com.example.linuxchat.bean.ChatMessage;
import com.loopj.android.image.SmartImage;
import com.loopj.android.image.SmartImageView;

import java.util.LinkedList;

/**
 * @author liulingjie
 * @date 20-6-18 下午5:48
 */
public class MessagesListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int LEFT=1;
    public static final int RIGHT=2;
    LinkedList<ChatMessage> messages=new LinkedList<ChatMessage>();

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if(viewType==LEFT){
            view= LayoutInflater.from(parent.getContext()).inflate(R.layout.left_message,parent,false);
        }else {
            view= LayoutInflater.from(parent.getContext()).inflate(R.layout.right_message,parent,false);
        }
        return new MessageItemView(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ChatMessage message=messages.get(position);
        Log.d("backmessage","绑定消息"+message.toString()+"\n");
        MessageItemView view=(MessageItemView)holder;
        view.headImage.setImageUrl(message.getImageUrl());
        view.name.setText(message.getName());
        view.content.setText(message.getContent());
    }

    @Override
    public int getItemViewType(int position) {
        String id=messages.get(position).getId();
        if(ChatActivity.USER_ID.equals(id))       //本人消息展示在右边
            return RIGHT;
        else
            return LEFT;
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    class MessageItemView extends RecyclerView.ViewHolder{
        SmartImageView headImage;
        TextView name,content;
        public MessageItemView(@NonNull View itemView) {
            super(itemView);
            headImage= itemView.findViewById(R.id.headImage);
            name=itemView.findViewById(R.id.name);
            content=itemView.findViewById(R.id.content);
        }
    }

    public void addLast(ChatMessage message){
        messages.addLast(message);
        notifyItemInserted(messages.size()-1);
    }

    public void addFirst(ChatMessage message){
        messages.addFirst(message);
        notifyItemInserted(0);
    }

    public int getFirstNumber(){
        return ((ChatMessage)messages.getFirst()).getNumber();
    }

    public int getLastNumber(){
        return ((ChatMessage)messages.getLast()).getNumber();
    }


//    public static void main(String[] args){
//        LinkedList<String> list=new LinkedList<String>();
//        list.add("11111");
//        list.add("22222");
//        list.add("33333");
//        list.add("44444");
//        list.add("55555");
//        list.add("66666");
//
//        list.addFirst("0000");
//        list.addLast("7777");
//
//        int i=0;
//        for(String o:list){
//            System.out.println(o);
//            System.out.println(list.get(i));
//            i++;
//        }
//
//        System.out.println(list.getLast());
//        System.out.println(list.getFirst());
//
//    }


}
